package com.mobilg.dating;

import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mobilg.client.xml.parser.MyXMLShopHandler;
import com.mobilg.client.xml.parser.ShopsList;

public class SaxXmlParser extends Activity{

	/** Create Object For SiteList Class */
	//SitesList sitesList = null;
	ShopsList shopsList = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main_new);

	/** Create a new layout to display the view */
	LinearLayout layout = new LinearLayout(this);
	layout.setOrientation(1);

	/** Create a new textview array to display the results */
	TextView name[];
	TextView identity[];
	TextView smallLogo[];
	TextView categoryIdentity[];

	try {

	/** Handling XML */
	SAXParserFactory spf = SAXParserFactory.newInstance();
	SAXParser sp = spf.newSAXParser();
	XMLReader xr = sp.getXMLReader();

	/** Send URL to parse XML Tags */
	/*URL sourceUrl = new URL(
	"http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");*/
	URL sourceUrl = new URL(
	"http://10.0.2.2:8080/AVMCepteServer/xml/624ecc2e855a266c1354f32b0addabb46d85fd7e/listShop");

	/** Create handler to handle XML Tags ( extends DefaultHandler ) */
	//MyXMLHandler myXMLHandler = new MyXMLHandler();
	MyXMLShopHandler myXMLHandler = new MyXMLShopHandler();
	
	xr.setContentHandler(myXMLHandler);
	xr.parse(new InputSource(sourceUrl.openStream()));

	} catch (Exception e) {
		System.out.println("XML Pasing Excpetion = " + e);
	}

	/** Get result from MyXMLHandler SitlesList Object */
	//sitesList = MyXMLHandler.sitesList;
	shopsList = MyXMLShopHandler.shopsList;

	
	/** Assign textview array lenght by arraylist size */
	identity = new TextView[shopsList.getName().size()];
	name = new TextView[shopsList.getName().size()];
	smallLogo = new TextView[shopsList.getName().size()];
	categoryIdentity = new TextView[shopsList.getName().size()];

	/** Set the result text in textview and add it to layout */
	for (int i = 0; i < shopsList.getName().size(); i++) {
		identity[i] = new TextView(this);
		identity[i].setText("Identity = "+shopsList.getIdentity().get(i));
		name[i] = new TextView(this);
		name[i].setText("Name = "+shopsList.getName().get(i));
		smallLogo[i] = new TextView(this);
		smallLogo[i].setText("Small Logo = "+shopsList.getLogo_small().get(i));
		categoryIdentity[i] = new TextView(this);
		categoryIdentity[i].setText("Category Identity = "+shopsList.getCategoryIdentity().get(i));

		layout.addView(identity[i]);
		layout.addView(name[i]);
		layout.addView(smallLogo[i]);
		layout.addView(categoryIdentity[i]);
	}

	/** Set the layout view to display */
	setContentView(layout);

	}
	
}
